function loadAllOrders(orderType, orderby) {// 默认加载时查询全部订单并默认排序

    $(".orderinfodetail").empty();
    $
            .ajax({
                url : "/DrivingMan/admin/getOrders.do",
                type : "post",
                data : {
                    "orderType" : orderType,
                    "orderby" : orderby
                },
                dataType : "json",
                success : function(result) {

                    if (result.status == "0000") {
                        var orderInfo = result.data;
                        for ( var i = 0; i < orderInfo.length; i++) {
                            var order = orderInfo[i];
                            var order_status = order.order_status;
                            var order_id = order.order_id;
                            var car_info = order.car_info;
                            var order_time = order.order_time;
                            var return_time = order.return_time;
                            if (order_status == "0") {
                                var tbody = '<tr>'
                                        + '<td>'
                                        + order_id
                                        + '</td>'
                                        + '<td>'
                                        + car_info
                                        + '</td>'
                                        + '<td>'
                                        + order_time
                                        + '</td>'
                                        + '<td>'
                                        + return_time
                                        + '</td>'
                                        + '<td>待接单</td>'
                                        + '<td><input type="button" id="acceptcar" onclick="dealorder(this)" value="接单"/><input type="button" id="acceptcar" onclick="dealorder(this)" value="拒绝"/></td>'
                                        + '</tr>';
                            }
                            else if (order_status == "1") {
                                var tbody = '<tr>' + '<td>' + order_id + '</td>' + '<td>' + car_info + '</td>' + '<td>'
                                        + order_time + '</td>' + '<td>' + return_time + '</td>' + '<td>进行中</td>'
                                        + '<td>等待用户还车</td>' + '</tr>';
                            }
                            else if (order_status == "2") {
                                var tbody = '<tr>' + '<td>' + order_id + '</td>' + '<td>' + car_info + '</td>' + '<td>'
                                        + order_time + '</td>' + '<td>' + return_time + '</td>' + '<td>已还车</td>'
                                        + '<td>用户尚未评价</td>' + '</tr>';
                            }
                            else if (order_status == "3") {
                                var tbody = '<tr>' + '<td>' + order_id + '</td>' + '<td>' + car_info + '</td>' + '<td>'
                                        + order_time + '</td>' + '<td>' + return_time + '</td>' + '<td>已完成</td>'
                                        + '<td>已完成</td>' + '</tr>';
                            }
                            else if (order_status == "4") {
                                var tbody = '<tr>' + '<td>' + order_id + '</td>' + '<td>' + car_info + '</td>' + '<td>'
                                        + order_time + '</td>' + '<td>' + return_time + '</td>' + '<td>无效订单</td>'
                                        + '<td>订单超时或拒绝</td>' + '</tr>';
                            }
                            $tbody = $(tbody);
                            $tbody.data("order_id", order_id);
                            $(".orderinfodetail").append($tbody);
                        }
                    }
                }
            });
}
function adminOrderList(a) {

    $(".checkon").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "checkon");
    var orderType = $(".checkon").find("a").text();
    var orderby = showAndHide(orderType);
    console.log(orderType, orderby);
    loadAllOrders(orderType, orderby);
}

function qryOrderListBy(a) {

    var orderType = $(".checkon").find("a").text();
    var orderby = null;
    if ("全部订单" == orderType) {
        $(".firsta").attr("class", "");
        var $a = $(a);
        $a.parent().attr("class", "firsta");
        orderby = $(".firsta").find("a").text();
    }
    if ("进行中订单" == orderType) {
        $(".firstb").attr("class", "");
        var $a = $(a);
        $a.parent().attr("class", "firstb");
        orderby = $(".firstb").find("a").text();
    }
    if ("已完成订单" == orderType) {
        $(".firstc").attr("class", "");
        var $a = $(a);
        $a.parent().attr("class", "firstc");
        orderby = $(".firstc").find("a").text();
    }
    console.log(orderType, orderby);
    loadAllOrders(orderType, orderby);
}

function manageCenter(a) {

    $(".checkon").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "checkon");
    var orderType = $(".checkon").find("a").text();
    var orderby = showAndHide(orderType);
    qryManageInfo(orderType, orderby);
}

function userOrderBy(a) {

    $(".firstd").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "firstd");
    var orderby = $(".firstd").find("a").text();
    var orderType = $(".checkon").find("a").text();
    qryManageInfo(orderType, orderby);
}

function carsOrderBy(a) {

    $(".firste").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "firste");
    var orderby = $(".firste").find("a").text();
    var orderType = $(".checkon").find("a").text();
    qryManageInfo(orderType, orderby);
}

function ordersOrderBy(a) {

    $(".firstf").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "firstf");
    var orderby = $(".firstf").find("a").text();
    var orderType = $(".checkon").find("a").text();
    qryManageInfo(orderType, orderby);
}

function tourOrderBy(a) {

    $(".firstg").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "firstg");
    var orderby = $(".firstg").find("a").text();
    var orderType = $(".checkon").find("a").text();
    qryManageInfo(orderType, orderby);
}

function qryManageInfo(orderType, orderby) {

    $("#usertbody").empty();
    $("#cartbody").empty();
    $("#ordertbody").empty();
    $("#tourtbody").empty();
    $
            .ajax({
                url : "/DrivingMan/admin/getManageInfo.do",
                type : "post",
                data : {
                    "orderType" : orderType,
                    "orderby" : orderby
                },
                dataType : "json",
                success : function(result) {

                    if (result.status == "0000") {
                        var resMap = result.data;
                        console.log(resMap);
                        if ("会员管理" == orderType) {
                            for ( var i = 0; i < resMap.length; i++) {
                                var res = resMap[i];
                                var tbody = '<tr><td>'
                                        + res.user_id
                                        + '</td><td>'
                                        + res.real_name
                                        + '</td>'
                                        + '<td>'
                                        + res.link_phone
                                        + '</td><td>'
                                        + res.cert_num
                                        + '</td>'
                                        + '<td>'
                                        + res.log_time
                                        + '</td>'
                                        + '<td><input type="button" value="删除" onclick="deluser(this)" class="adminBut"/></td></tr>';
                                $tbody = $(tbody);
                                $tbody.data("user_id", res.user_id);
                                $("#usertbody").append($tbody);
                            }
                        }
                        if ("车辆管理" == orderType) {
                            for ( var i = 0; i < resMap.length; i++) {
                                var res = resMap[i];
                                var tbody = '<tr>' + '<td>' + res.carid + '</td>' + '<td>' + res.pinpai + '</td>'
                                        + '<td>' + res.chexingname + '</td>' + '<td>' + res.jibie + '</td>' + '<td>'
                                        + res.carprice + '万</td>'
                                        + '<td><input type="button" value="删除" onclick="delcars(' + res.carid
                                        + ')" class="adminBut"/></td>' + '</tr>';
                                $("#cartbody").append(tbody);
                            }
                        }
                        if ("订单管理" == orderType) {
                            for ( var i = 0; i < resMap.length; i++) {
                                var res = resMap[i];
                                console.log(res);
                                var tbody = '<tr>' + '<td>' + res.order_id + '</td>' + '<td>' + res.car_info + '</td>'
                                        + '<td>' + res.order_time + '</td>' + '<td>' + res.return_time + '</td>'
                                        + '<td>' + res.order_status + '</td>'
                                        + '<td><input type="button" value="删除" onclick="delorder(' + res.order_id
                                        + ')" class="adminBut"/></td>' + '</tr>';
                                $("#ordertbody").append(tbody);
                            }
                        }
                        if ("体验中心" == orderType) {
                            for ( var i = 0; i < resMap.length; i++) {
                                var res = resMap[i];
                                console.log(res);
                                var tbody = '<tr>' + '<td>' + res.article_id + '</td>'
                                        + '<td><a href="Javascript:void(0)" name="' + res.article_id
                                        + '" class="newstitle" onclick="getTestRole(this)">' + res.article_title
                                        + '</a></td>' + '<td>' + res.article_time + '</td>' + '<td>' + res.user_name
                                        + '</td>' + '<td><input type="button" value="删除" onclick="deltour('
                                        + res.article_id + ')" class="adminBut"/></td>' + '</tr>';
                                $("#tourtbody").append(tbody);
                            }
                        }
                    }
                }
            });
}

function addCars(a) {

    $(".checkon").attr("class", "");
    var $a = $(a);
    $a.parent().attr("class", "checkon");
    var orderType = $(".checkon").find("a").text();
    showAndHide(orderType);
}

function addImg() {

    var carPicPath = $("#carpic").val();
    console.log(carPicPath);
    if (carPicPath == "") {
        alert("请先选择图片!");
        return;
    }
    $.ajaxFileUpload({
        url : "/DrivingMan/carInfo/uploadImg.do", // 需要链接到服务器地址
        secureuri : false,
        fileElementId : "carpic", // 文件选择框的id属性（必须）
        dataType : "json",
        success : function(result) {

            alert(result.detail);
            if (result.status == "0000") {
                var carId = result.data;
                console.log(carId);
                $("#carid").html(carId);
            }
        },
        error : function(result) {

            alert("图片上传失败,请重试");
        }
    });
}

function addcarInfo() {

    var carid = $("#carid").text();
    var pinpai = $("#Select1  option:selected").text();
    var chexing = $("#Select2  option:selected").text();
    var chexingname = $("#Select3  option:selected").text();
    var carprice = $("#carprice").val();
    var jibie = $("#jibie option:selected").text();
    var fadongji = $("#pailiang").val() + $("#torl option:selected").text().substring(0, 1) + $("#maxml").val() + "马力";// 排量+增压方式+最大马力
    var biansuxiang = $("#biansuxiang option:selected").text();
    var cheshen = $("#cheshen option:selected").text();
    var ckg = $("#chang").val() + "*" + $("#kuan").val() + "*" + $("#gao").val();
    var zhouju = $("#zhouju").val();

    var youxiang = $("#youxiangrongji").val();
    var zuoweishu = $("#sitenum").val();
    var pailiang = $("#pailiang").val();
    var jinqi = $("#jinqi option:selected").text();
    var qigang = $("#qigang").val();
    var maxmali = $("#maxml").val();
    var maxnj = $("#maxnj").val();
    var ranyoutype = $("#ranyoutype option:selected").text();
    var ranhoubianhao = $("#ranhoubianhao option:selected").text();
    var gongyou = $("#gongyou option:selected").text();
    var paifang = $("#paifang option:selected").text();

    var qudong = $("#qudong option:selected").text();
    var zhuli = $("#zhuli option:selected").text();
    var qianxuangua = $("#qianxuangua option:selected").text();
    var houxuangua = $("#houxuangua option:selected").text();
    var qianzhidong = $("#qianzhidong option:selected").text();
    var houzhidong = $("#houzhidong option:selected").text();
    var zhuchezhidong = $("#zhuchezhidong option:selected").text();
    var yuanguangdeng = $("#yuanguangdeng option:selected").text();
    var jinguangdeng = $("#jinguangdeng option:selected").text();
    console.log(carid, pinpai, chexing, chexingname, carprice, jibie, fadongji, biansuxiang, cheshen, ckg, zhouju);
    console.log(youxiang, zuoweishu, pailiang, jinqi, qigang, maxmali, maxnj, ranyoutype, ranhoubianhao, gongyou,
            paifang);
    console.log(qudong, zhuli, qianxuangua, houxuangua, houzhidong, qianzhidong, zhuchezhidong, yuanguangdeng,
            jinguangdeng);
    $.ajax({
        url : "/DrivingMan/carInfo/addCarsInfo.do",
        type : "post",
        data : {
            "carid" : carid,
            "carprice" : carprice,
            "pinpai" : pinpai,
            "chexing" : chexing,
            "chexingname" : chexingname,
            "jibie" : jibie,
            "fadongji" : fadongji,
            "biansuxiang" : biansuxiang,
            "cheshen" : cheshen,
            "ckg" : ckg,
            "zhouju" : zhouju,
            "youxiang" : youxiang,
            "zuoweishu" : zuoweishu,
            "pailiang" : pailiang,
            "jinqi" : jinqi,
            "qigang" : qigang,
            "maxmali" : maxmali,
            "maxnj" : maxnj,
            "ranyoutype" : ranyoutype,
            "ranhoubianhao" : ranhoubianhao,
            "gongyou" : gongyou,
            "paifang" : paifang,
            "qudong" : qudong,
            "zhuli" : zhuli,
            "qianxuangua" : qianxuangua,
            "houxuangua" : houxuangua,
            "qianzhidong" : qianzhidong,
            "houzhidong" : houzhidong,
            "zhuchezhidong" : zhuchezhidong,
            "yuanguangdeng" : yuanguangdeng,
            "jinguangdeng" : jinguangdeng
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert("添加车辆信息成功");
                window.location.reload();
            }
        },
        error : function() {

        }
    });
}
function addCarInfo() {

    var carid = $("#carid").text();
    console.log(carid);
    var carprice = $("#carprice").text();
    var pinpai = $("#pinpai").text();
    var chexing = $("#chexing").text();
    var chexingname = $("#chexingname").text();
    var jibie = $("#jibie").text();
    var fadongji = $("#fadongji").text();
    var biansuxiang = $("#biansuxiang").text();
    var cheshen = $("#cheshen").text();
    var ckg = $("#ckg").text();
    var zhouju = $("#zhouju").text();
    var youxiang = $("#youxiang").text();
    var zuoweishu = $("#zuoweishu").text();
    var pailiang = $("#pailiang").text();
    var jinqi = $("#jinqi").text();
    var qigang = $("#qigang").text();
    var maxmali = $("#maxmali").text();
    var maxnj = $("#maxnj").text();
    var ranyoutype = $("#ranyoutype").text();
    var ranhoubianhao = $("#ranhoubianhao").text();
    var gongyou = $("#gongyou").text();
    var paifang = $("#paifang").text();
    var qudong = $("#qudong").text();
    var zhuli = $("#zhuli").text();
    var qianxuangua = $("#qianxuangua").text();
    var houxuangua = $("#houxuangua").text();
    var qianzhidong = $("#qianzhidong").text();
    var houzhidong = $("#houzhidong").text();
    var zhuchezhidong = $("#zhuchezhidong").text();
    var yuanguangdeng = $("#yuanguangdeng").text();
    var jinguangdeng = $("#jinguangdeng").text();

    $.ajax({
        url : "/DrivingMan/carInfo/addCarsInfo.do",
        type : "post",
        data : {
            "carid" : carid,
            "carprice" : carprice,
            "pinpai" : pinpai,
            "chexing" : chexing,
            "chexingname" : chexingname,
            "jibie" : jibie,
            "fadongji" : fadongji,
            "biansuxiang" : biansuxiang,
            "cheshen" : cheshen,
            "ckg" : ckg,
            "zhouju" : zhouju,
            "youxiang" : youxiang,
            "zuoweishu" : zuoweishu,
            "pailiang" : pailiang,
            "jinqi" : jinqi,
            "qigang" : qigang,
            "maxmali" : maxmali,
            "maxnj" : maxnj,
            "ranyoutype" : ranyoutype,
            "ranhoubianhao" : ranhoubianhao,
            "gongyou" : gongyou,
            "paifang" : paifang,
            "qudong" : qudong,
            "zhuli" : zhuli,
            "qianxuangua" : qianxuangua,
            "houxuangua" : houxuangua,
            "qianzhidong" : qianzhidong,
            "houzhidong" : houzhidong,
            "zhuchezhidong" : zhuchezhidong,
            "yuanguangdeng" : yuanguangdeng,
            "jinguangdeng" : jinguangdeng
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert("添加车辆信息成功");
            }
        },
        error : function() {

        }
    });
}

function showAndHide(orderType) {

    if (orderType == "全部订单") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "block";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firsta").find("a").text();
        return orderby;
    }
    if (orderType == "进行中订单") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "block";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firstb").find("a").text();
        return orderby;
    }
    if (orderType == "已完成订单") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "block";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firstc").find("a").text();
        return orderby;
    }
    if (orderType == "会员管理") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "block";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firstd").find("a").text();
        return orderby;
    }
    if (orderType == "车辆管理") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "block";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firste").find("a").text();
        return orderby;
    }
    if (orderType == "订单管理") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "block";
        g.style.display = "none";
        h.style.display = "none";
        var orderby = $(".firstf").find("a").text();
        return orderby;
    }
    if (orderType == "体验中心") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "block";
        h.style.display = "none";
        var orderby = $(".firstg").find("a").text();
        return orderby;
    }
    if (orderType == "新增车辆") {
        var a = document.getElementById("allorder");
        var b = document.getElementById("onorder");
        var c = document.getElementById("endorder");
        var d = document.getElementById("deluser");
        var e = document.getElementById("delcars");
        var f = document.getElementById("delorders");
        var g = document.getElementById("deltour");
        var h = document.getElementById("addcars");
        a.style.display = "none";
        b.style.display = "none";
        c.style.display = "none";
        d.style.display = "none";
        e.style.display = "none";
        f.style.display = "none";
        g.style.display = "none";
        h.style.display = "block";
    }
}

function dealorder(a) {

    var tag = $(a).val();
    var order_id = $(a).parent().parent().data("order_id");
    console.log(tag, order_id);
    $.ajax({
        url : "/DrivingMan/admin/changeOrderStatus.do",
        type : "post",
        data : {
            "tag" : tag,
            "order_id" : order_id
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.reload();
            }
        }
    });
}

function deluser(a) {

    var user_id = $(a).parent().parent().data("user_id");
    $.ajax({
        url : "/DrivingMan/admin/deluser.do",
        type : "post",
        data : {
            "user_id" : user_id
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.location.reload();
                showAndHide(会员管理);
            }
        }
    });
}
function delcars(a) {

    $.ajax({
        url : "/DrivingMan/admin/delcars.do",
        type : "post",
        data : {
            "car_id" : a
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.location.reload();
                showAndHide(车辆管理);
            }
        }
    });
}
function delorder(a) {

    $.ajax({
        url : "/DrivingMan/admin/delorder.do",
        type : "post",
        data : {
            "order_id" : a
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.location.reload();
                showAndHide("订单管理");
            }
        }
    });
}
function deltour(a) {

    $.ajax({
        url : "/DrivingMan/admin/deltour.do",
        type : "post",
        data : {
            "art_id" : a
        },
        dataType : "json",
        success : function(result) {

            if (result.status == "0000") {
                alert(result.detail);
                window.location.reload();
                showAndHide("体验中心");
            }
        }
    });
}
